package com.mt.vsphere.business.service;

import com.mt.vsphere.business.bean.base.VCenterCatalogue;
import com.mt.vsphere.business.bean.entity.AreaItemInfo;
import com.mt.vsphere.business.bean.entity.RoomItemInfo;
import com.mt.vsphere.business.bean.enums.SysCatalogueType;
import com.mt.vsphere.business.dao.AreaItemInfoDao;
import com.mt.vsphere.business.dao.RoomItemInfoDao;
import com.mt.vsphere.business.vsphere.VCenterDataHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class VSphereService {
    @Autowired
    private RoomItemInfoDao roomItemInfoDao;

    @Autowired
    private AreaItemInfoDao areaItemInfoDao;

    private List<RoomItemInfo> roomItemInfos;

    private Map<String, VCenterDataHelper> vcenterConnnecMap;
    @PostConstruct
    public void initConnect(){
        vcenterConnnecMap = new HashMap<>();
        roomItemInfos = (List<RoomItemInfo>) roomItemInfoDao.findAll();
        roomItemInfos.forEach(item->{
            vcenterConnnecMap.put(item.getName(),new VCenterDataHelper(item.getVCenterAddress(),item.getUsername(),item.getPwd()));
        });
    }


    public List<VCenterCatalogue> getAllCatalogue(){
        List<VCenterCatalogue> list = new ArrayList<>();
        for (AreaItemInfo areaItemInfo:areaItemInfoDao.findAll()){
            List<VCenterCatalogue> catalogues = new ArrayList<>();
            for (RoomItemInfo roomItemInfo : roomItemInfoDao.findByAreaId(areaItemInfo.getId())){
                catalogues.add(new VCenterCatalogue(SysCatalogueType.Room.num,roomItemInfo.getName(),
                        vcenterConnnecMap.get(roomItemInfo.getName()).getCatalogues()));
            }
            list.add(new VCenterCatalogue(SysCatalogueType.Area.num,areaItemInfo.getName(),catalogues));
        }
        return list;
    }
    public Object getVirtualMachineAttr(String roomName,String type,String value){
        return vcenterConnnecMap.get(roomName).getVirtualMachineAttr(type, value);
    }




}
